//
//  CreateClassVC.m
//  XTeng
//
//  Created by 周成龙 on 16/8/17.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "CreateClassVC.h"

#import "VDJConstant.h"
#import "Header.h"
#import "TreeHeader.h"

#define kNameLabelTag 10000
#define kTextFieldTag 20000
#define kImageViewTag 30000

@interface CreateClassVC ()<UITableViewDelegate, UITableViewDataSource>

{
    NSArray *_dataSource;
    NSMutableArray *_selectedCateArray;
}
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *contentArray;


@end

@implementation CreateClassVC
- (UITableView *)tableView
{
    if (nil == _tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kClearClolor;
        //        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = kLineColor;
        _tableView.separatorInset = UIEdgeInsetsMake(0,kMargin, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"创建班级";
    
    self.contentArray = @[@{@"name":@"班级名称", @"value":@""}, @{@"name":@"上传头像", @"value":@""}];
    
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout.spaceToSuperView(UIEdgeInsetsMake([self getStartOriginY], 0, 0, 0));
    
    [self initCreatButton];
    
    _selectedCateArray = [[NSMutableArray alloc] init];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ClassMember.plist" ofType:nil];
    _dataSource = [[NSArray alloc] initWithContentsOfFile:plistPath];
}

- (void)initCreatButton
{
    UIView *view = [UIView new];
    view.backgroundColor = kClearClolor;
    self.tableView.tableFooterView = view;
    view.sd_layout
    .leftSpaceToView(self.tableView, 0)
    .rightSpaceToView(self.tableView, 0)
    .topSpaceToView(self.tableView, 0)
    .heightIs(50);
    
    UIButton *btn = [UIButton new];
    [btn setBackgroundImage:[UIImage imageNamed:@"按钮长"] forState:UIControlStateNormal];
    [btn setTitle:@"添加班级成员" forState:UIControlStateNormal];
    btn.titleLabel.font = kFontWithSize(14);
    [btn addTarget:self action:@selector(creatButton) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    btn.sd_layout
    .leftSpaceToView(view, kMargin)
    .rightSpaceToView(view, kMargin)
    .topSpaceToView(view, 10)
    .heightIs(35);
}

- (void)creatButton
{
    MyLog(@"add class member ---");
    TreeSelectTableViewController *multiSelectTableView;
    TreeDataSource *data2;
    
    data2 = [[TreeDataSource alloc] initWithArray:_dataSource SelectCategory:_selectedCateArray];
    data2.chooseCount = 4;
    multiSelectTableView  = [[TreeSelectTableViewController alloc] initWithTreeDataSource:data2 IsSingleSelect:NO ResultBlock:^(NSArray *arrResult){
        
        [_selectedCateArray removeAllObjects];
        if (arrResult.count == 0)
        {
            MyLog(@"----开始了");
        }else
        {
            NSMutableArray *selectCategoryName = [[NSMutableArray alloc] init];
            for(int i = 0;i < arrResult.count;i++)
            {
                NSDictionary *categoryDic=[arrResult objectAtIndex:i];
                NSString *selectCategoryID = [categoryDic objectForKey:@"id"];
                [selectCategoryName addObject:[categoryDic objectForKey:@"name"]];
                [_selectedCateArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:selectCategoryID,@"id", nil]];
            }
            MyLog(@"----回来了");
//            [self.label2 setText:[selectCategoryName componentsJoinedByString:@","]];
        }
    }];
    
    [self.navigationController pushViewController:multiSelectTableView animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
        CGFloat h = 20;
        
        UILabel *nameLabel = [UILabel labelWithFont:14 withTextColor:kBlackFontColor withBackgroundColor:kClearClolor withTextAlignment:0 withText:@"name" withNumberLines:1];
        nameLabel.tag = kNameLabelTag;
        nameLabel.frame = CGRectMake(kMargin, 11, 100, h);
        [cell.contentView addSubview:nameLabel];
        
        if (indexPath.row == 0) {
            UITextField *textField = [UITextField textFieldWithFont:14 withTextColor:kBlackFontColor withBackgroundColor:kClearClolor withKeyBoardType:UIKeyboardTypeDefault withEextAlignment:NSTextAlignmentRight withClearButtonMode:UITextFieldViewModeWhileEditing withText:@"" withPlaceholder:@"请输入班级名称" withPlaceholderTextColor:kGrayFontColor];
            textField.tag = kTextFieldTag;
            textField.frame = CGRectMake(kScreenWidth - 200 - kMargin, 11, 200, h);
            [cell.contentView addSubview:textField];
        }else {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.frame = CGRectMake(kScreenWidth - 25 - 16 - kMargin, 10, 25, 25);
            imageView.backgroundColor = kRedFontColor;
            imageView.tag = kImageViewTag;
            [cell.contentView addSubview:imageView];
            imageView.layer.cornerRadius = 12.5;
            imageView.layer.masksToBounds = YES;
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    NSDictionary *dict = self.contentArray[indexPath.row];
    
    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:kNameLabelTag];
    nameLabel.text = dict[@"name"];
    
    UITextField *textField = (UITextField *)[cell.contentView viewWithTag:kTextFieldTag];
    textField.text = dict[@"value"];
    
    return cell;
}


@end
